<template>
   <el-form label-width="100px">

      <el-form-item label="SKU名称">
         <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
      </el-form-item>

      <el-form-item label="价格(元)">
         <el-input placeholder="价格(元)" type="number" v-model="skuParams.price"></el-input>
      </el-form-item>

      <el-form-item label="重量(克)">
         <el-input placeholder="重量(克)" type="number" v-model="skuParams.weight"> </el-input>
      </el-form-item>

      <el-form-item label="描述">
         <el-input placeholder="描述" type="text" v-model="skuParams.skuDesc"></el-input>
      </el-form-item>

      <el-form-item label="平台属性">
         <el-form :inline="true">
            <el-form-item v-for="(item, index) in attrArr" :key="item.id" :label="item.attrName">
               <el-select v-model="item.attrIdAndValueId">
                  <el-option v-for="(attrValue, index) in item.attrValueList" :label="attrValue.valueName"
                     :value="`${item.id}:${attrValue.id}   `" :key="attrValue.id"></el-option>

               </el-select>
            </el-form-item>

         </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
         <el-form :inline="true">
            <el-form-item v-for="(item, index) in spuHasSaleAttr" :key="item.id" :label="item.saleAttrName">
               <el-select v-model="item.saleIdAndValueId">
                  <el-option :value="`${item.id}:${attrValue.id}`" v-for="(attrValue, index) in item.spuSaleAttrValueList"
                     :label="attrValue.saleAttrValueName" :key="attrValue.id"></el-option>

               </el-select>
            </el-form-item>

         </el-form>
      </el-form-item>

      <el-form-item label="图片名称">
         <el-table border :data="imgArr" ref="table">
            <el-table-column type="selection" width="100px" align="center"></el-table-column>
            <el-table-column label="图片">
               <template #default="{ row, $index }">
                  <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
               </template>
            </el-table-column>
            <el-table-column prop="imgName" label="名称"></el-table-column>
            <el-table-column label="操作">
               <template #default="{ row, $index }">
                  <el-button type="primary" size="small" @click="handler(row)">设置默认</el-button>
               </template>
            </el-table-column>
         </el-table>

      </el-form-item>
      <el-form-item>
         <el-button type="primary" @click="save">保存</el-button>
         <el-button type="primary" @click="cancel">取消</el-button>
      </el-form-item>
   </el-form>
</template>

<script setup lang="ts">
// 引用请求api
import { reqAttr } from '@/api/product/attr';
import { reqSpuImageList, reqSpuHasSaleAttr, reqAddSku } from '@/api/product/spu';
import { SkuData } from '@/api/product/spu/type';
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
let attrArr = ref<any>([]);

let spuHasSaleAttr = ref<any>([]);
let imgArr = ref<any>([]);
let table = ref<any>([]);


// 收集SKU的参数
let skuParams = reactive<SkuData>({
   category3Id: "", //三级分类的ID
   spuId: "", //已有的SPU的ID
   tmId: "", //SPU品牌的ID
   // model收集
   skuName: "", //sku名字
   price: "", //sku价格
   weight: "", //sku重量
   skuDesc: "", //sku的描述


   skuAttrValueList: [],
   skuSaleAttrValueList: [],
   skuDefaultImg: "" //sku图片地址
})

const cancel = () => {

   $emit('changeScene', { flag: 0, params: 'update' })
}

const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: any) => {
   console.log('initSkuData', c1Id, c2Id, spu)


   skuParams.category3Id = spu.category3Id
   skuParams.spuId = spu.id
   skuParams.tmId = spu.tmId

   // 获取平台属性
   let result: any = await reqAttr(c1Id, c2Id, spu.category3Id);
   if (result.code == 200) {
      attrArr.value = result.data
   }
   // 获取对应的销售属性
   let result2: any = await reqSpuHasSaleAttr(spu.id)
   if (result2.code == 200) {
      spuHasSaleAttr.value = result2.data
   }

   let result3: any = await reqSpuImageList(spu.id)
   if (result3.code == 200) {
      imgArr.value = result3.data
   }
}
// 设置默认图片
const handler = (row: any) => {

   imgArr.value.forEach((item: any) => {
      table.value.toggleRowSelection(item, item == row)
   })
   // console.info(table.value)
   // table.value.toggleRowSelection(row, true)
   skuParams.skuDefaultImg = row.imgUrl;

}

// 对外暴露方法
defineExpose({ initSkuData })

// 父组件传过的方法
let $emit = defineEmits(['changeScene'])

const save = async () => {
   // 整理参数
   // 平台属性

   skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, item: any) => {
      if (item.attrIdAndValueId) {
         const [attrId, valueId] = item.attrIdAndValueId.split(':')
         prev.push({ attrId, valueId })
      }
      return prev
   }, [])
   // 销售属性
   skuParams.skuSaleAttrValueList = spuHasSaleAttr.value.reduce((prev: any, item: any) => {
      if (item.saleIdAndValueId) {
         const [saleAttrId, saleAttrValueId] = item.saleIdAndValueId.split(':')
         prev.push({ saleAttrId, saleAttrValueId })
      } 
      return prev
   }, [])
   // 图片
   // skuParams.skuDefaultImg ;
   // 发请求
   // 发送请求

  let result :any = await reqAddSku(skuParams) 
  if(result.code == 200){
   ElMessage.success( '添加SKU成功')
   $emit('changeScene',{flag:0,params:''})
  }else{
   ElMessage.error('添加SKU失败')
  }

}
</script>

<script  lang="ts">
export default {
   name: 'SkuForm'
}
</script>
<style scoped></style>